package android.tusnotas.activities;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.tusnotas.activities.extras.ShowCourseActivityExtras;
import android.tusnotas.beans.Course;
import android.tusnotas.beans.Grade;
import android.tusnotas.util.Devel;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class ShowCourse extends CommonActivity {

	private Course course = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
		setContentView(R.layout.show_course);
		Integer result = drawActivity(getIntent());
		Log.w(this.getClass().getName(),"INTEGER RESULT"+result);
	}
	
	private Integer drawActivity(Intent intent){
		
		Integer courseId = intent.getIntExtra(ShowCourseActivityExtras.COURSE_ID, -1);
		intent.removeExtra(ShowCourseActivityExtras.COURSE_ID);
		if (courseId > 0) {
			getIntent().removeExtra(ShowCourseActivityExtras.COURSE_ID);
			this.course = tusNotasModel.selectCoursesById(courseId);
			
			((TextView) findViewById(R.id.courseName)).setText(course.getName());
			((TextView) findViewById(R.id.classroom)).setText(getResources().getString(R.string.label_classroom)+" "+course.getClassroom());
			((TextView) findViewById(R.id.timetable)).setText(course.getDays() + " | " + course.getTimetable());
			((TextView) findViewById(R.id.exam_date)).setText(course.getExamenDate().split(" ")[0]);
			((TextView) findViewById(R.id.attendance)).setText(getResources().getString(R.string.label_attendance)+" "+course.getAttendance()+" %");
			
			TableLayout tableLayout = (TableLayout) findViewById(R.id.gradesTable);
			tableLayout.removeAllViews();
			
			TableRow tableRow = null;
			TextView textViewName = null;
			TextView textViewValue = null;
			
			ViewGroup.LayoutParams layoutParams = ((TextView) findViewById(R.id.classroom)).getLayoutParams();
			ViewGroup.LayoutParams topRowLayoutParams = ((TableRow) findViewById(R.id.generalInfoRow1)).getLayoutParams();
			ViewGroup.LayoutParams bottomRowLayoutParams = ((TableRow) findViewById(R.id.generalInfoRow2)).getLayoutParams();
			
			for (Grade grade : tusNotasModel.selectGradesByCourse(course.getId())) {
				
				tableRow = new TableRow(getApplicationContext());
				
				tableRow.setLayoutParams(topRowLayoutParams);
				tableRow.setBackgroundDrawable(getResources().getDrawable(R.drawable.course_table_element));
				
				textViewName = new TextView(getApplicationContext());
				textViewValue = new TextView(getApplicationContext());
				
				textViewName.setLayoutParams(layoutParams);
				textViewValue.setLayoutParams(layoutParams);
				
				textViewName.setGravity(Gravity.CENTER_VERTICAL);
				textViewValue.setGravity(Gravity.CENTER_VERTICAL);
				
				textViewName.setText(grade.getActivity());
				textViewName.setTextColor(Color.BLACK);
				textViewValue.setText(grade.getGrade());
				textViewValue.setTextColor(Color.BLACK);
				
				tableRow.addView(textViewName);
				tableRow.addView(textViewValue);
				
				tableLayout.addView(tableRow);
			}
			
			tableRow.setLayoutParams(bottomRowLayoutParams);
			tableRow.setBackgroundDrawable(getResources().getDrawable(R.drawable.course_table_element_bottom));
			
			tableLayout.getChildAt(0).setBackgroundDrawable(getResources().getDrawable(R.drawable.course_table_element_top));
			
			return 1;
		} 
			return -1;
	}

    public void gotoDownloads(View view){
    	Intent showDownloadsIntent =  new Intent(getApplicationContext(), ShowFolders.class);
    	showDownloadsIntent.putExtra(ShowCourseActivityExtras.COURSE_ID, course.getId());
		startActivity(showDownloadsIntent);
    }
	
    public void gotoNews(View view){
    	Intent showNewsIntent =  new Intent(getApplicationContext(), ShowNews.class);
    	showNewsIntent.putExtra(ShowCourseActivityExtras.COURSE_ID, course.getId());
		startActivity(showNewsIntent);
    }
    
    public void gotoGroup(View view){
    	Intent showGroupIntent =  new Intent(getApplicationContext(), ShowGroup.class);
    	showGroupIntent.putExtra(ShowCourseActivityExtras.COURSE_ID, course.getId());
		startActivity(showGroupIntent);
    }
    
	@Override
	public void onNewIntent(Intent intent){
		super.onNewIntent(intent);
		if (Devel.fullDevel || Devel.showInstancesDevel){
			Log.w(this.getClass().getName(), "New Intent on Show Course");
		}
		drawActivity(intent);
	}
}
